<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/3/27
 * Time: 16:21
 */

namespace App\Repositories;


use App\Models\Wxcard;
use App\Models\Wxuser;
use App\Models\WxuserWxcard;
use function foo\func;
use Illuminate\Support\Facades\Log;

class IndexRepository
{
    protected $wxuser_model;
    protected $wxcard_model;
    protected $wx_user_card_model;

    public function __construct(Wxuser $wxuser,Wxcard $wxcard,WxuserWxcard $wx_user_card)
    {
        $this->wxuser_model = $wxuser;
        $this->wxcard_model = $wxcard;
        $this->wx_user_card_model = $wx_user_card;
    }

    /*
     * 更新或者保存用户
     */
    public function CreateOrUpdateWxuser($user_info)
    {
        $wxuser_data['nickname'] = $user_info['nickname']??'';
        $wxuser_data['gender'] = $user_info['sex']??0;
        $wxuser_data['avatar'] = $user_info['headimgurl']??'';
        $wxuser_data['province'] = $user_info['province']??'';
        $wxuser_data['country'] = $user_info['country']??'';
        $wxuser_data['city'] = $user_info['city']??'';
        $wxuser_data['lanaguage'] = $user_info['language']??'';
        $wxuser_data['openid'] = $user_info['openid']??'';
        $wxuser_data['unionid'] = $user_info['unionid'];

        $user = $this->wxuser_model->where('unionid', $wxuser_data['unionid'])->first();

        if(!empty($user)){
            if(!empty($wxuser_data['nickname'])) $user->update($wxuser_data);
        }else{
            $user = $this->wxuser_model->create($wxuser_data);
        }

        return $user;
    }

    /*
     * 获取微信卡券
     */
    public function getWxCardByUid()
    {
        //获取用户已领取的id_card记录
        $user = session(config('services.wechat.wauth_session_name'));
        
        $records = $this->wx_user_card_model->where('id_wxusers',$user->id)->pluck('id_wxcards')->toArray();

        $cards_used = $this->wxcard_model->whereIn('card_id',$records)->get();

        if(empty($records) || empty($cards_used)){

            $card = $this->wxcard_model->where('sku','>','0')->first()->toArray();
        }

        $card = $this->wxcard_model->whereNotIn('card_id',$cards_used)->first()->toArray();

        if(empty($card))
            $card = $this->wxcard_model->where('sku','>','0')->first()->toArray();

        return json_encode(array_merge($card,['wxuser_id'=>$user->id]));

    }

    /*
     * 获取历史记录
     */
    public function getUserCardHistory()
    {
        $card = app('EasyWechatOfficial')->card;
        //$wxuser = Wxuser::find(2);
        /*return session(config('services.wechat.wauth_session_name'))->GetCardRecord()->get();*/

        //卡券信息
        //$cardInfo = app('EasyWechatOfficial')->card->get('p1cWJuLhbhlD-D0cGP2QiFae6ZNo');
        //dd($cardInfo);
        //已获得卡券类表coe
        //$list = app('EasyWechatOfficial')->card->getUserCards('o1cWJuABF_6UQ7Q0LZRNhNWMPpYo', 'p1cWJuLhbhlD-D0cGP2QiFae6ZNo');
//dd($list);
        //查询卡券code
        //$code_list = $card->code->getDepositedCount('p1cWJuLhbhlD-D0cGP2QiFae6ZNo');
        //核销
        //$hexiao = $card->code->consume('957485189744');
        //$encryptedCode = 'XXIzTtMqCxwOaawoE91+VJdsFmv7b8g0VZIZkqf4GWA60Fzpc8ksZ/5ZZ0DVkXdE';

        //$result = $card->code->decrypt($encryptedCode);
//dd($result);
        //是否核销
        //$couse = $card->code->get('957485189744', 'p1cWJuLhbhlD-D0cGP2QiFae6ZNo', $checkConsume = true);
//dd($couse);
        //$wxuser = Wxuser::find(2);
        //$card = $wxuser->GetCardRecord()->with('wxcard')->groupBy('id_wxcards')->get();
//dd($card);
        $cards = session(config('services.wechat.wauth_session_name'))->GetCardRecord()->with('wxcard')->get();

        $new_cards = [];

        foreach ($cards as $k=>$v){
            //dd($v);
            $dec_result = $card->code->decrypt($v->code);//获取解码值
            //获取核销信息
            $card_consume = $card->code->get($dec_result['code'], $v->wxcard->card_id, $checkConsume = true);

            if($card_consume['errcode']==0){

                $new_cards[$k]['status'] = $v->getCardStatus($card_consume['can_consume']);
                $new_cards[$k]['time'] = date('Y-m-d',strtotime($v->created_at));
                $new_cards[$k]['time_status'] = $v->getCardTimeStatus($card_consume['card']['end_time']);

            }elseif ($card_consume['errcode']== 40127){

                $new_cards[$k]['status'] = ['status'=>0,'desc'=>'已过期'];
                $new_cards[$k]['time'] = date('Y-m-d',strtotime($v->created_at));
                $new_cards[$k]['time_status'] = ['status'=>0,'desc'=>'已过期'];
            }

            $new_cards[$k]['logo'] = $v->wxcard->logo_url;
            $new_cards[$k]['title'] = $v->wxcard->title;
        }

        return $new_cards;
    }
}